package com.dongnam.quanly.common.dao;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.dongnam.quanly.common.ActionCategory;
import com.dongnam.quanly.common.ActionType;
import com.dongnam.quanly.common.ObjectType;
import com.dongnam.quanly.common.ScreenId;
import com.dongnam.quanly.common.model.AuditTrailLog;

public class AuditTrailLogDao extends GenericDao<AuditTrailLog, Long> {

	private List<String> availableSortFields = Arrays.asList(new String[] { "actionTime", "actioneeName", "actioneeIp",
			"screenId", "actionCategory", "actionType", "result", "object", "objectId" });

	public long findCountByFilters(Timestamp fromTime, Timestamp toTime, String actioneeName, String actioneeIp,
			ScreenId screenId, ActionCategory actionCategory, ActionType actionType, String result, ObjectType object,
			String objectId) {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		if (fromTime != null && toTime != null) {
			crit = crit.add(Restrictions.between("actionTime", fromTime, new Timestamp(toTime.getTime() + 86399999L)));
		} else if (fromTime != null) {
			crit = crit.add(Restrictions.ge("actionTime", fromTime));
		} else if (toTime != null) {
			crit = crit.add(Restrictions.le("actionTime", new Timestamp(toTime.getTime() + 86399999L)));
		}
		if (actioneeName != null && !actioneeName.equals("")) {
			crit = crit.add(Restrictions.like("actioneeName", "%" + actioneeName + "%"));
		}
		if (actioneeIp != null && !actioneeIp.equals("")) {
			crit = crit.add(Restrictions.like("actioneeIp", "%" + actioneeIp + "%"));
		}
		if (screenId != null) {
			crit = crit.add(Restrictions.eq("screenId", screenId));
		}
		if (actionCategory != null) {
			crit = crit.add(Restrictions.eq("actionCategory", actionCategory));
		}
		if (actionType != null) {
			crit = crit.add(Restrictions.eq("actionType", actionType));
		}
		if (result != null && !result.equals("")) {
			crit = crit.add(Restrictions.like("result", "%" + result + "%"));
		}
		if (object != null) {
			crit = crit.add(Restrictions.eq("object", object));
		}
		if (objectId != null && !objectId.equals("")) {
			crit = crit.add(Restrictions.like("objectId", "%" + objectId + "%"));
		}
		// if(details != null && !details.equals("")){
		// crit = crit.add(Restrictions.like("details", "%" + details + "%"));
		// }
		return ((Number) crit.setProjection(Projections.rowCount()).uniqueResult()).longValue();
	}

	@SuppressWarnings("unchecked")
	public List<AuditTrailLog> findByFilters(int pageSize, int pageNumber, Timestamp fromTime, Timestamp toTime,
			String actioneeName, String actioneeIp, ScreenId screenId, ActionCategory actionCategory,
			ActionType actionType, String result, ObjectType object, String objectId, String sortOrder, String sortField) {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		if (fromTime != null && toTime != null) {
			crit = crit.add(Restrictions.between("actionTime", fromTime, new Timestamp(toTime.getTime())));
		} else if (fromTime != null) {
			crit = crit.add(Restrictions.ge("actionTime", fromTime));
		} else if (toTime != null) {
			crit = crit.add(Restrictions.le("actionTime", new Timestamp(toTime.getTime() + 86399999L)));
		}
		if (actioneeName != null && !actioneeName.equals("")) {
			crit = crit.add(Restrictions.like("actioneeName", "%" + actioneeName + "%"));
		}
		if (actioneeIp != null && !actioneeIp.equals("")) {
			crit = crit.add(Restrictions.like("actioneeIp", "%" + actioneeIp + "%"));
		}
		if (screenId != null) {
			crit = crit.add(Restrictions.eq("screenId", screenId));
		}
		if (actionCategory != null) {
			crit = crit.add(Restrictions.eq("actionCategory", actionCategory));
		}
		if (actionType != null) {
			crit = crit.add(Restrictions.eq("actionType", actionType));
		}
		if (result != null && !result.equals("")) {
			crit = crit.add(Restrictions.like("result", "%" + result + "%"));
		}
		if (object != null) {
			crit = crit.add(Restrictions.eq("object", object));
		}
		if (objectId != null && !objectId.equals("")) {
			crit = crit.add(Restrictions.like("objectId", "%" + objectId + "%"));
		}
		// if(details != null && !details.equals("")){
		// crit = crit.add(Restrictions.like("details", "%" + details + "%"));
		// }
		crit.setFirstResult(pageNumber * pageSize);
		crit.setMaxResults(pageSize);
		if (sortOrder != null && !sortOrder.equalsIgnoreCase("UNSORTED") && sortField != null
				&& availableSortFields.contains(sortField)) {
			if (sortOrder.equalsIgnoreCase("ASCENDING")) {
				crit.addOrder(Order.asc(sortField));
			} else {
				crit.addOrder(Order.desc(sortField));
			}
		}
		// System.out.println("list: "+crit.list());
		return crit.list();
	}

}
